package battleship;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;

import battleship.view.Console;
import battleship.view.GUI;
import battleship.view.Views;

public class Controller {



	private static Views GUI;

	public static GUI guiWindow;
	public static Console consoleWindow;
	public static ViewController controller;

	private static List<PluginSetup> pluginsSetup;
	private static List<PluginAI> pluginsAI;
	private static List<PluginLoop> pluginsLoop;
	private static List<PluginEnd> pluginsEnd;
	private static List<PluginBackground> pluginsBackground;

	public static GUI getWindow()
	{
		return guiWindow;
	}

	public static Console getConsole()
	{
		return consoleWindow;
	}

	public static ViewController getController()
	{
		return controller;
	}

	public static List<PluginSetup> getPluginsSetup()
	{
		if(pluginsSetup == null)
		{
			pluginsSetup = new ArrayList<PluginSetup>();
		}
		return pluginsSetup;
	}

	public static List<PluginAI> getPluginsAI()
	{
		if(pluginsAI == null)
		{
			pluginsAI = new ArrayList<PluginAI>();
		}
		return pluginsAI;
	}

	public static List<PluginLoop> getPluginsLoop()
	{
		if(pluginsLoop == null)
		{
			pluginsLoop = new ArrayList<PluginLoop>();
		}
		return pluginsLoop;
	}

	public static List<PluginEnd> getPluginsEnd()
	{
		if (pluginsEnd == null)
		{
			pluginsEnd = new ArrayList<PluginEnd>();
		}
		return pluginsEnd;
	}

	public static List<PluginBackground> getPluginsBackground()
	{
		if (pluginsBackground == null)
		{
			pluginsBackground = new ArrayList<PluginBackground>();
		}
		return pluginsBackground;
	}

	public static void main(String[] args) {


		/*
		 * Setup phase
		 */
		setGUI();


		if(GUI == Views.GUI)
		{
			guiWindow = new GUI();
			
			final JMenuItem newGame = new JMenuItem("nieuw spel");
			
			guiWindow.getFileMenu().add(newGame);
			
			newGame.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
			
			newGame.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					guiWindow.dispose();
					guiWindow = new GUI();
					guiWindow.getFileMenu().add(newGame);
				}
			});
		}
		else if(GUI == Views.Console)
		{
			consoleWindow = new Console();
			controller = new ViewController();
			consoleWindow.start();
		}
		else
		{
			System.exit(1);
		}




	}

	public static Views getGUI() {
		return GUI;
	}

	private static void setGUI()
	{
		Object[] options = { "Console", "GUI",
		};
		int GUI = JOptionPane.showOptionDialog(null,
				"Welke versie wil je spelen?",
				"zeeslag",
				JOptionPane.YES_NO_OPTION,
				JOptionPane.QUESTION_MESSAGE,
				null,     //do not use a custom Icon
				options,  //the titles of buttons
				options[0]); //default button title

		if (GUI == 1)
		{
			Controller.GUI = Views.GUI;
		}
		else
		{
			Controller.GUI = Views.Console;
		}
	}
}
